<?php

namespace App\Admin\Selectable;

use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Selectable;
use App\Models\yth\CustomerModel;
use App\Models\yth\CustomerTypeModel;
use App\Models\DataDictionaryModel;

class CustomerSelect extends Selectable
{
    public $model = CustomerModel::class;

    public function make()
    {
        $this->model()->where(['status'=>1])->orderBy('created_at','desc');

        $this->column('type_id','客户分类')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : CustomerTypeModel::selectValue('id',$filed);
        });

        $this->column('customer_name','客户名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $this->column('mobile','客户电话')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $this->column('address','客户地址')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $this->column('opening_balance','期初往来余额')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $this->column('current_balance','当前余额')->display(function($filed){
            return empty($filed) ? 0 : $filed;
        });

        $this->column('created_at','操作时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        $this->column('status','状态')->using(DataDictionaryModel::Status, '未知')->label([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $this->column('id','消费记录')->display(function($filed){
            return '<span data-toggle="modal" data-target="#grid-modal-3-goods_parts" data-key="'.$filed.'">
               <a href="/admin/customer/account_record?id='.$filed.'"><i class="fa fa-clone"></i>&nbsp;&nbsp;消费记录</a>
            </span>';
        });

        $this->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('customer_name','客户名称');
                $filter->equal('type_id', '客户分类')->select(CustomerTypeModel::selectOptions());
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('mobile','客户电话');
                $filter->between('created_at', '时间选择')->datetime();
            });
            $filter->disableIdFilter();
        });
    }
}